<template>
    <div class="page-header">
      <h1>作品收藏</h1>
    </div>

    <div class="search-section">
        <el-input
          v-model="searchQuery"
          placeholder="Search..."
          class="search-input"
        >
          <template #suffix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-button class="menu-button">
          <el-icon><Menu /></el-icon>
        </el-button>
      </div>

    <div class="works-collection-container">
      <div class="main-image">
        <img :src="selectedWork.image" :alt="selectedWork.title" />
        <div class="image-info">
          <h2>{{ selectedWork.title }}</h2>
          <p class="author">{{ selectedWork.author }}</p>
          <p class="description">{{ selectedWork.description }}</p>
        </div>
      </div>
    </div>
  
    <!-- 工具栏 -->
    <div class="toolbar">
      <button @click="download">下载</button>
      <button 
        @click="toggleFullscreen"
        :class="{ active: isFullscreen }"
      >
        {{ isFullscreen ? '退出全屏' : '全屏' }}
      </button>
      <!-- <button @click="toggleLock">锁屏</button> -->
      <!-- <button @click="readPost">读帖</button> -->
      <button @click="toggleCharacter">单字</button>
      <!-- <button @click="shareWorksCollection">分享</button> -->
    </div>
  
    <!-- 当前作品指示点 -->
    <!--
    <div class="dot-indicator">
      <span
        v-for="(work, index) in works"
        :key="work.id"
        class="dot"
        :class="{ active: selectedWork.id === work.id }"
      ></span>
    </div>
    -->
  
    <!-- 固定在页面底部的缩略图区域 -->
    <div class="thumbnail-bar">
      <div
        v-for="(work, index) in works"
        :key="work.id"
        class="thumbnail"
        :class="{ selected: selectedWork.id === work.id }"
        @click="selectWork(work)"
        :style="getThumbnailStyle(index)"
      >
        <img :src="work.image" :alt="work.title" />
      </div>
    </div>
  </template>
  
  <script setup lang="ts">
  import { ref, onMounted, onUnmounted } from 'vue';
  import { saveAs } from 'file-saver';
  
  // 定义 Work 类型
  interface Work {
    id: number;
    title: string;
    author: string;
    description: string;
    image: string;
  }
  
  // 示例数据
  const works = ref<Work[]>([
    {
      id: 1,
      title: '兰亭集序',
      author: '王羲之（东晋）',
      description: '东晋永和九年，著名的书法作品。',
      image: 'https://s1.mintiny.com/wlake/i/2024/12/22/690in.png',
    },
    {
      id: 2,
      title: '黄州寒食帖',
      author: '苏轼（北宋）',
      description: '苏轼在黄州被贬期间所作，情感深沉。',
      image: 'https://s1.mintiny.com/wlake/i/2024/12/22/68n1r.png',
    },
    {
      id: 3,
      title: '十七帖',
      author: '王羲之（东晋）',
      description: '此帖为一组书信，据考证是写给他朋友益州刺史周抚的。',
      image: 'https://s1.mintiny.com/wlake/i/2024/12/22/oa5.png',
    },
  ]);
  
  // 选中的作品，默认选第一个
  const selectedWork = ref<Work>(works.value[0]);
  
  // 选择作品的方法
  const selectWork = (work: Work) => {
    console.log('Selected work:', work);
    selectedWork.value = work;
  };
  
  // 获取缩略图的样式（用于动态调整）
  const getThumbnailStyle = (index: number) => {
    const isSelected = selectedWork.value.id === works.value[index].id;
    return {
      transform: isSelected ? 'scale(1.3)' : 'scale(1)',
      opacity: isSelected ? 1 : 0.6,
      transition: 'transform 0.3s ease, opacity 0.3s ease',
      zIndex: isSelected ? 10 : 1,
    };
  };
  
  // 下载功能实现
  const download = async () => {
    try {
      // 获取当前选中作品的图片URL
      const imageUrl = selectedWork.value.image;
      
      // 从URL获取图片数据
      const response = await fetch(imageUrl);
      const blob = await response.blob();
      
      // 使用 file-saver 下载图片
      const fileName = `${selectedWork.value.title}-${selectedWork.value.author}.png`;
      saveAs(blob, fileName);
    } catch (error) {
      console.error('下载失败:', error);
      // 这里可以添加错误提示
    }
  };
  
  // 全屏功能实现
  const isFullscreen = ref(false);
  
  const toggleFullscreen = async () => {
    try {
      const elem = document.querySelector('.main-image') as HTMLElement;
      
      if (!document.fullscreenElement) {
        await elem.requestFullscreen();
        isFullscreen.value = true;
      } else {
        await document.exitFullscreen();
        isFullscreen.value = false;
      }
    } catch (error) {
      console.error('全屏切换失败:', error);
      // 这里可以添加错误提示
    }
  };
  
  // 监听全屏变化
  onMounted(() => {
    document.addEventListener('fullscreenchange', () => {
      isFullscreen.value = !!document.fullscreenElement;
    });
  });
  
  onUnmounted(() => {
    document.removeEventListener('fullscreenchange', () => {
      isFullscreen.value = !!document.fullscreenElement;
    });
  });
  
  const toggleOverlay = () => {
    console.log('遮盖');
  };
  
  const toggleLock = () => {
    console.log('锁屏');
  };
  
  const readPost = () => {
    console.log('读帖');
  };
  
  const toggleCharacter = () => {
    console.log('单字');
  };
  
  const toggleAnnotation = () => {
    console.log('释文');
  };

  const searchQuery = ref('');
  </script>
    <style scoped>
  .page-header {
    text-align: center;
  }
  
  .page-header h1 {
    font-size: 36px;
    font-weight: bold;
    color: #333;
    margin: 0;
  }

  .search-section {
    margin: 20px ;
  display: flex;
  align-items: center;
  gap: 10px;
  width: 500px;
  margin-bottom: 30px;
  background-color: #fff;
  border-radius: 50px;
  padding: 5px 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}


  
  .works-collection-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    max-width: 1200px;
    padding-bottom: 0px;
    justify-content: center;
    min-height: 40vh;
    margin: 0 auto;
    width: 100%;
  }
  
  .main-image {
    display: flex;
    flex-direction: row;
    align-items: center;
    text-align: left;
    margin-bottom: 20px;
    max-width: 1000px;
    width: 100%;
    justify-content: center;
    margin-left: auto;
    margin-right: auto;
  }
  
  .main-image img {
    max-width: 50%; /* 占据容器一半宽度 */
    max-height: 500px;
    width: auto;
    height: auto;
    display: block;
    margin-right: 20px; /* 添加图片与文字之间的间距 */
  }
  
  .image-info {
    flex: 1; /* 让文字区域自适应剩余空间 */
  }
  
  .image-info h2 {
    font-size: 24px;
    margin: 10px 0 5px;
    color: #333;
  }
  
  .image-info .author {
    font-size: 14px;
    color: #666;
  }
  
  .image-info .description {
    font-size: 12px;
    color: #888;
    margin-top: 8px;
  }
  
  .dot-indicator {
    display: none; /* 隐藏点指示器 */
  }
  
  .dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background-color: #ccc;
    transition: background-color 0.3s, transform 0.3s;
  }
  
  .dot.active {
    background-color: #3498db;
    transform: scale(1.3);
  }
  
  /* 工具栏样式 */
  .toolbar {
    display: flex;
    justify-content: center;
    gap: 20px;
    padding: 10px;
    background-color: #f4f4f4;
    border-radius: 8px;
  }
  
  .toolbar button {
  padding: 8px 15px;
  font-size: 14px;
  background-color: transparent; /* 去除填充色 */
  color: #3498db; /* 文字颜色 */
  border: 2px solid #3498db; /* 边框颜色与文字一致 */
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s, color 0.3s; /* 添加过渡效果 */
    }

    .toolbar button:hover {
    background-color: #3498db; /* 鼠标悬停时背景色 */
    color: white; /* 鼠标悬停时文字颜色 */
    }

  
  /* 固定在底部的缩略图区域 */
  .thumbnail-bar {
    display: flex;
    gap: 20px;
    padding: 20px 0;
    width: 100%;
    max-width: 800px;
    justify-content: center;
    position: fixed;
    bottom: 50px;
    left: 50%;
    transform: translateX(-50%); /* 让缩略图栏居中 */
    background: none; /* 去掉背景 */
    overflow-x: auto; /* 使缩略图区域可以水平滚动 */
    scroll-behavior: smooth; /* 平滑滚动 */
  }
  
  .thumbnail {
    width: 110px;
    height: 90px;
    border: 2px solid transparent;
    border-radius: 12px;
    cursor: pointer;
    transition: transform 0.3s, opacity 0.3s, z-index 0s;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-shrink: 0;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* 添加阴影效果 */
  }
  
  .thumbnail img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 10px;
  }
  
  .thumbnail:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2); /* 悬停时的阴影效果 */
  }
  
  .thumbnail.selected {
    border-color: #3498db;
    box-shadow: 0 6px 12px rgba(52, 152, 219, 0.4); /* 高亮选中的缩略图 */
  }
  
  @media (max-width: 768px) {
    .thumbnail-bar {
      gap: 16px; /* 缩小间距 */
    }
  
    .thumbnail {
      width: 90px;
      height: 70px;
    }
  }

  .main-image {
    /* ... 现有样式 ... */
  }

  .main-image:fullscreen {
    background: white;
    padding: 40px;
    display: flex;
    justify-content: flex-start;  /* 改为靠左对齐 */
    align-items: center;
    width: 100vw;
    height: 100vh;
    gap: 40px;  /* 添加间距 */
  }

  .main-image:fullscreen img {
    max-width: 85%;  /* 增加图片显示比例 */
    max-height: 90vh;
    object-fit: contain;
    margin-left: 40px;  /* 左侧留出空间 */
  }

  .main-image:fullscreen .image-info {
    position: relative;  /* 改为相对定位 */
    right: auto;
    top: auto;
    transform: none;
    background: white;  /* 改为纯白背景 */
    padding: 20px;
    border-radius: 8px;
    box-shadow: none;  /* 移除阴影 */
    width: 300px;  /* 固定宽度 */
    min-width: auto;  /* 移除最小宽度限制 */
  }

  .main-image:fullscreen .image-info h2 {
    font-size: 28px;
    margin-bottom: 15px;
    color: #333;
  }

  .main-image:fullscreen .image-info .author {
    font-size: 18px;
    margin-bottom: 10px;
    color: #666;
  }

  .main-image:fullscreen .image-info .description {
    font-size: 16px;
    line-height: 1.6;
    color: #888;
  }

  /* 为按钮添加激活状态样式 */
  .toolbar button.active {
    background-color: #3498db;
    color: white;
  }
  </style>
  
